//
//  LCHomeStatusesCell.swift
//  SinaWeiBo
//
//  Created by mac on 16/12/5.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit
//  自定义微博cell

/*
 1. 原创微博视图
 2. 转发微博视图
 3. 底部的ToolBar
 
 */

class LCHomeStatusesCell: UITableViewCell {
    
    
    //定义一个全局的约束来记录需要修改的约束，在修改之前先移除掉
    private var currentConstraint: Constraint?
    
    
    //1懒加载原创微博视图
    private lazy var originalView: LCHomeOriginalView = LCHomeOriginalView()
    //2懒加载toolbar视图
    private lazy var toolBarView: LCHomeToolBarView = LCHomeToolBarView()
    //3懒加载转发视图
    private lazy var reweeedView: LCHomeRetweetedView = LCHomeRetweetedView()
    
    //接收控制器传过来的数据
    var cellViewModel: LCHomeCellViewModel?{
        //在接收到cell传过来的数据的时候就给他的三个子视图赋值
        didSet{
            //给原创微博绑定数据
            originalView.originalModel = cellViewModel
            //给toolBar赋值
            toolBarView.statusessModel = cellViewModel
            //给转发视图传递数据
            reweeedView.retweetModel = cellViewModel
            
            //判断是否有转发数据,如果有就设置转发view的top和原创view，如果没有就设置toolbarview的top与原创view的约束
            //1先移除已有的约束
            currentConstraint?.uninstall()
            //更新约束
            if cellViewModel?.statusess?.retweeted_status != nil{
                toolBarView.snp_updateConstraints(closure: { (make) in
                    currentConstraint = make.top.equalTo(reweeedView.snp_bottom).constraint
                })
                //设置显示
                reweeedView.isHidden = false
    
            }else{
                toolBarView.snp_updateConstraints(closure: { (make) in
                    currentConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                //设置隐藏
                reweeedView.isHidden = true
            }
            
        }
    }
    
    //cell重写initstype方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //布局子控件
    func setupUI(){
        contentView.backgroundColor = UIColor.init(white: 0.95, alpha: 1)
        //添加视图
        contentView.addSubview(originalView)
        contentView.addSubview(toolBarView)
        contentView.addSubview(reweeedView)
        
        //设置约束
        originalView.snp_makeConstraints { (make) in
            make.top.equalTo(self.contentView).offset(8)
            make.left.right.equalTo(self.contentView)
        }
        toolBarView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(reweeedView)
            make.bottom.equalTo(contentView)
            currentConstraint = make.top.equalTo(reweeedView.snp_bottom).constraint
            make.height.equalTo(35)
        }
        reweeedView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
        }
        
        
    }
    

}
